package com.yycx.module.bus.provider.service.hy.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.hy.BusErpOrderDetails;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.enums.VoucherTypeEnum;
import com.yycx.module.bus.provider.mapper.hy.HyRemoteOrderMapper;
import com.yycx.module.bus.provider.service.BusCustomBalanceService;
import com.yycx.module.bus.provider.service.BusCustomBankService;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.hy.BusErpOrderDetailsService;
import com.yycx.module.bus.provider.service.hy.HyRemoteOrderService;
import com.yycx.module.bus.provider.service.lc.SyncVoucherService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 海鹰销售单接口实现类
 *
 * @author flyme
 * @date 2020-08-17
 */
@Service
@DS("hy")
public class HyRemoteOrderServiceImpl extends BaseServiceImpl<HyRemoteOrderMapper, HyOrder> implements HyRemoteOrderService {


    @Autowired
    private HyRemoteOrderMapper hyRemoteOrderMapper;

    @Autowired
    @Lazy
    private BusCustomBalanceService customBalanceService;

    @Autowired
    SyncVoucherService syncService;

    @Lazy
    @Autowired
    private BusErpOrderDetailsService busErpOrderDetailsService;

    /**
     * 入订单表分页列表
     *
     * @param params
     * @return
     */
    @Override
    @DS("hy")
    public ResultBody hyOrderList(Map params) {
        int limit = TypeUtils.castToInt(params.get("limit"));
        int page = TypeUtils.castToInt(params.get("page"));
        int endLimit = limit * (page - 1);
        params.put("endLimit", endLimit);
        List<EntityMap> list = hyRemoteOrderMapper.hyOrderList(params);
        EntityMap cq = new EntityMap();
        cq.put("单据编码", params.get("orderNo"));
        cq.put("单据日期", params.get("beginDate"));
        cq.put("单据日期", params.get("endDate"));
        int total = hyRemoteOrderMapper.countHyOrder(cq);
        Integer pages = Math.toIntExact(total / limit);
        EntityMap pageModel = new EntityMap();
        pageModel.put("size", list.size());
        pageModel.put("total", total);
        pageModel.put("current", page);
        pageModel.put("pages", pages);
        pageModel.put("records", list);
        return ResultBody.ok(pageModel);
    }

    /**
     * 入订单详情
     *
     * @param orderNo
     * @return
     */
    @Override
    @DS("hy")
    public EntityMap getHyOrderById(String orderNo) {
        Map params = new HashMap(5);
        params.put("orderNo", orderNo);
        return hyRemoteOrderMapper.getHyOrderById(params);
    }


    /**
     * 入订单明细
     *
     * @param orderNo
     * @return
     */
    @Override
    @DS("hy")
    public List<EntityMap> queryHyOrderDetailList(String orderNo) {
        return hyRemoteOrderMapper.queryHyOrderDetailList(orderNo);
    }


    /**
     * 出订单分页列表
     *
     * @param openUser
     * @param params
     * @return
     */
    @Override
    @DS("hy")
    public ResultBody outOrderPageList(String dbName, Map params) {
        EntityMap pageModel = new EntityMap();
        int limit = MapUtil.getInt(params, "limit");
        int page = MapUtil.getInt(params, "page");
        int endLimit = limit * (page - 1);
        params.put("endLimit", endLimit);
        List<EntityMap> list = hyRemoteOrderMapper.outOrderPageList(dbName, params);
        EntityMap cq = new EntityMap();
        cq.put("单据编码", params.get("orderNo"));
        cq.put("单据日期", params.get("beginDate"));
        cq.put("单据日期", params.get("endDate"));
        int total = hyRemoteOrderMapper.countHyOutOrder(dbName, cq);
        Integer pages = Math.toIntExact(total / limit);
        pageModel.put("size", list.size());
        pageModel.put("total", total);
        pageModel.put("current", page);
        pageModel.put("pages", pages);
        pageModel.put("records", list);
        return ResultBody.ok(pageModel);
    }

    @Override
    @DS("hy")
    public EntityMap getOutOrderById(String dbName, String orderNo) {
        Map params = new HashMap(3);
        params.put("orderNo", orderNo);
        return hyRemoteOrderMapper.getOutOrderById(dbName, params);
    }

    @Override
    @DS("hy")
    public EntityMap getOutOrderAllFieldById(String dbName, String orderNo) {
        Map params = new HashMap(3);
        params.put("orderNo", orderNo);
        return hyRemoteOrderMapper.getOutOrderAllFieldById(dbName, params);
    }


    @Override
    @DS("hy")
    public List<EntityMap> queryOutHyOrderDetailList(String dbName, String orderNo) {
        return hyRemoteOrderMapper.queryOutHyOrderDetailList(dbName, orderNo);
    }

    @Override
    @DS("hy")
    public EntityMap getCustomByCode(String customCode) {
        return hyRemoteOrderMapper.getCustomByCode(customCode);
    }

    @Override
    public List<EntityMap> getHistoryDrug(String dbName, String drugNo, String customCode) {
        return hyRemoteOrderMapper.getHistoryDrug(dbName, drugNo, customCode);
    }

    @Override
    @DS("hy")
    public EntityMap getProductByCode(String productCode) {
        return hyRemoteOrderMapper.getProductByCode(productCode);
    }

    @Override
    @DS("hy")
    public List<EntityMap> getProductByType(String dbName, String productType) {
        return hyRemoteOrderMapper.getProductByType(dbName, productType);
    }

    @Override
    @DS("hy")
    public List<EntityMap> saleOrderList(String dbName, String currentDay, List<String> orderNos) {
        return hyRemoteOrderMapper.saleOrderList(dbName, currentDay, orderNos, null);
    }

    @Override
    @DS("hy")
    public List<EntityMap> saleOrderList(String dbName, String currentDay, String orderNo) {
        return hyRemoteOrderMapper.saleOrderList(dbName, currentDay, Arrays.asList(), orderNo);
        //return hyRemoteOrderMapper.saleOrderList1(dbName, currentDay,  orderNo);
    }

    @Override
    public List<EntityMap> saleOrderList(String dbName, String startDay, String endDay, List<String> orderNo) {
        return hyRemoteOrderMapper.saleOrderLists(dbName, startDay, endDay, orderNo);
    }

    @Override
    public long saleOrderCount(String dbName, String currentDay) {
        return hyRemoteOrderMapper.saleOrderCount(dbName, currentDay).longValue();
    }

    @Override
    @DS("hy")
    public List<EntityMap> saleOrderDetailList(String dbName, String currentDay, List<String> orderNos) {
        return hyRemoteOrderMapper.saleOrderDetailList(dbName, currentDay, orderNos, null);
    }


    @Override
    public List<EntityMap> saleOrderDetailList(String dbName, String currentDay, String orderNo) {
        return hyRemoteOrderMapper.saleOrderDetailList(dbName, currentDay, Arrays.asList(), orderNo);
    }

    @Override
    @DS("hy")
    public List<EntityMap> buyOrderList(String dbName, String currentDay, List<String> orderNos) {
        return hyRemoteOrderMapper.buyOrderList(dbName, currentDay, orderNos, null);
    }

    @Override
    public List<EntityMap> buyOrderList(String dbName, String currentDay, String orderNo) {
        return hyRemoteOrderMapper.buyOrderList(dbName, currentDay, Arrays.asList(), orderNo);
    }

    @Override
    public List<EntityMap> buyOrderList(String dbName, String startDay, String endDay, List<String> orderNo) {
        return hyRemoteOrderMapper.buyOrderLists(dbName, startDay, endDay, orderNo);
    }

    @Override
    public List<EntityMap> buyOrderDetailList(String dbName, String currentDay, List<String> orderNos) {
        return hyRemoteOrderMapper.buyOrderDetailList(dbName, currentDay, orderNos, null);
    }

    @Override
    public List<EntityMap> buyOrderDetailList(String dbName, String currentDay, String orderNo) {
        return hyRemoteOrderMapper.buyOrderDetailList(dbName, currentDay, Arrays.asList(), orderNo);
    }

    @Override
    public long buyOrderCount(String dbName, String currentDay) {
        return hyRemoteOrderMapper.buyOrderCount(dbName, currentDay);
    }

    @Override
    public List<EntityMap> sampleOrderDetailList(String dbName, String month) {
        return hyRemoteOrderMapper.sampleOrderDetailList(dbName, month);
    }

    @Override
    public List<EntityMap> sampleOrderList(String dbName, String month) {
        return hyRemoteOrderMapper.sampleOrderList(dbName, month);
    }

    @Override
    public List<EntityMap> saleOrderDetail(String dbName, String month, List<String> orderNos) {
        return hyRemoteOrderMapper.saleOrderDetail(dbName, month, orderNos);
    }

    @Override
    public List<EntityMap> saleCostList(String dbName, String month, List<String> orderNos) {
        return hyRemoteOrderMapper.saleCostList(dbName, month, orderNos, null);
    }

    @Override
    public List<EntityMap> saleCostList(String dbName, String startDay, String endDay, List<String> orderNo) {
        return hyRemoteOrderMapper.saleCostLists(dbName, startDay, endDay, orderNo);
    }

    @Override
    public List<EntityMap> saleCostList(String dbName, String currentDay, String orderNo) {
        return hyRemoteOrderMapper.saleCostList(dbName, currentDay, Arrays.asList(), orderNo);
    }

    @Override
    public List<EntityMap> saleCostDetailList(String dbName, String month, List<String> orderNos) {
        return hyRemoteOrderMapper.saleCostDetailList(dbName, month, orderNos, null);
    }

    @Override
    public List<EntityMap> saleCostDetailList(String dbName, String month, String orderNo) {
        return hyRemoteOrderMapper.saleCostDetailList(dbName, month, Arrays.asList(), orderNo);
    }

    @Override
    public long saleCostCount(String dbName, String currentDay) {
        return hyRemoteOrderMapper.saleCostCount(dbName, currentDay);
    }

    @Override
    public List<EntityMap> getBuyProductByBatch(String dbName, String productCode, String productBatch) {
        return hyRemoteOrderMapper.getBuyProductByBatch(dbName, productCode, productBatch);
    }

    @Override
    public List<EntityMap> getSaleProductByBatch(String dbName, String productCode, String productBatch, String orderDate) {
        return hyRemoteOrderMapper.getSaleProductByBatch(dbName, productCode, productBatch, orderDate);
    }

    @Override
    public List<EntityMap> receiptPaymentOrderList(String fx, String dbName, String currentDay, List<String> orderNos) {
        return hyRemoteOrderMapper.receiptPaymentOrderList(fx, dbName, currentDay, orderNos);
    }

    @Override
    public List<EntityMap> receiptPaymentOrderList(String fx, String dbName, String startDay, String endDay, List<String> orderNos) {
        return hyRemoteOrderMapper.receiptPaymentOrderLists(fx, dbName, startDay, endDay, orderNos);
    }

    @Override
    public List<EntityMap> purchasePaymentOrderList(String dbName, String currentDay, List<String> orderNos) {
        return hyRemoteOrderMapper.purchasePaymentOrderList(dbName, orderNos);
    }


    @Override
    public List<EntityMap> purchasePaymentOrderDetailList(String erpName, String currentDay, List<String> orderNos) {
        return hyRemoteOrderMapper.purchasePaymentOrderDetailList(erpName, orderNos);
    }

    @Override
    public List<EntityMap> purchasePaymentDetailList(String erpName, String mxNo, String productCode, String productBatch) {
        return hyRemoteOrderMapper.purchasePaymentDetailList(erpName, mxNo);
    }


    @Override
    public List<EntityMap> purchasePayableOrderList(String dbName, String currentDay, List<String> orderNos) {
        return hyRemoteOrderMapper.purchasePayableOrderList(dbName, currentDay, orderNos);
    }

    @Override
    public List<EntityMap> purchasePayableOrderDetailList(String dbName, String currentDay, List<String> orderNos) {
        return hyRemoteOrderMapper.purchasePayableOrderDetailList(dbName, currentDay, orderNos);
    }

    @Override
    public List<EntityMap> selectHyDrugList(String dbName) {
        return hyRemoteOrderMapper.selectHyDrugList(dbName);
    }

    @Override
    public List<EntityMap> selectTop10HyDrugList(String dbName, String title, List<String> drugCodes) {
        return hyRemoteOrderMapper.selectTop10HyDrugList(dbName, title, drugCodes);
    }

    @Override
    public List<EntityMap> selectTop10HyDrugListNew(String dbName, String title, List<String> drugCodes) {
        return hyRemoteOrderMapper.selectTop10HyDrugListNew(dbName, title, drugCodes);
    }

    @Override
    public List<EntityMap> selectStoreCode(String dbName, List<String> storeNameList) {
        return hyRemoteOrderMapper.selectStoreCode(dbName, storeNameList);
    }

    @Override
    public List<EntityMap> selectDrugCodes(String dbName, List<String> storeCodeList) {
        return hyRemoteOrderMapper.selectDrugCodes(dbName, storeCodeList);
    }

    @Override
    public List<EntityMap> selectTop20HyCustomList(String dbName, String title) {
        List<EntityMap> list = hyRemoteOrderMapper.selectTop20HyCustomList(dbName, title);
        Date nowDate = DateUtils.addDays(DateUtils.getNowDate(), -1);
        Long compayId = OpenHelper.getCompanyId();
        if (FlymeUtils.isNotEmpty(list)) {
            for (EntityMap datum : list) {
                String customCode = datum.get("customCode");
                //测试账套不检测
                if (!dbName.equals("CBYY_LXZT.dbo.")) {
                    Boolean allowSelect = false;
                    //质量证书有效期
                    Date zlDate = datum.get("zlDate");
                    Boolean zlDateTag = true;
                    if (FlymeUtils.isNotEmpty(zlDate) && zlDate.before(nowDate)) {
                        zlDateTag = false;
                    }
                    datum.put("zlDateTag", zlDateTag);
                    //药品许可有效期
                    Date ypDate = datum.get("ypDate");
                    Boolean ypDateTag = true;
                    if (FlymeUtils.isNotEmpty(ypDate) && ypDate.before(nowDate)) {
                        ypDateTag = false;
                    }
                    datum.put("ypDateTag", ypDateTag);
                    //授权有效期
                    Date sqDate = datum.get("sqDate");
                    Boolean sqDateTag = true;
                    if (FlymeUtils.isNotEmpty(sqDate) && sqDate.before(nowDate)) {
                        sqDateTag = false;
                    }
                    datum.put("sqDateTag", sqDateTag);
                    //身份证有效期
                    Date sfzDate = datum.get("sfzDate");
                    Boolean sfzDateTag = true;
                    if (FlymeUtils.isNotEmpty(sfzDate) && sfzDate.before(nowDate)) {
                        sfzDateTag = false;
                    }
                    datum.put("sfzDateTag", sfzDateTag);
                    //质保协议有效期
                    Date zbDate = datum.get("zbDate");
                    Boolean zbDateTag = true;
                    if (FlymeUtils.isNotEmpty(zbDate) && zbDate.before(nowDate)) {
                        zbDateTag = false;
                    }
                    datum.put("zbDateTag", zbDateTag);
                    //器械许可有效期
                    Date qxDate = datum.get("qxDate");
                    Boolean qxDateTag = true;
                    if (FlymeUtils.isNotEmpty(qxDate) && qxDate.before(nowDate)) {
                        qxDateTag = false;
                    }
                    datum.put("qxDateTag", qxDateTag);
                    //年度公示期限
                    Date ngDate = datum.get("ngDate");
                    Boolean ngDateTag = true;
                    if (FlymeUtils.isNotEmpty(ngDate) && ngDate.before(nowDate)) {
                        ngDateTag = false;
                    }
                    datum.put("ngDateTag", ngDateTag);
                    //执照有效期
                    Date zzDate = datum.get("zzDate");
                    Boolean zzDateTag = true;
                    if (FlymeUtils.isNotEmpty(zzDate) && zzDate.before(nowDate)) {
                        zzDateTag = false;
                    }
                    datum.put("zzDateTag", zzDateTag);

                    //供销有效期
                    Date gxDate = datum.get("gxDate");
                    Boolean gxDateTag = true;
                    if (FlymeUtils.isNotEmpty(gxDate) && gxDate.before(nowDate)) {
                        gxDateTag = false;
                    }
                    datum.put("gxDateTag", gxDateTag);
                    //提货人委托书期限
                    Date wtsDate = datum.get("wtsDate");
                    Boolean wtsDateTag = true;
                    if (FlymeUtils.isNotEmpty(wtsDate) && wtsDate.before(nowDate)) {
                        wtsDateTag = false;
                    }
                    datum.put("wtsDateTag", wtsDateTag);
                    //全部符合
                    if (zzDateTag && ngDateTag && qxDateTag && zbDateTag && sfzDateTag && sqDateTag && ypDateTag && zlDateTag && gxDateTag && wtsDateTag) {
                        allowSelect = true;
                    }
                    datum.put("allowSelect", allowSelect);
                } else {
                    datum.put("allowSelect", true);
                }
            }
        }
        return list;
    }


    @Override
    public List<EntityMap> selectHyDeptList(String dbName) {
        return hyRemoteOrderMapper.selectHyDeptList(dbName);
    }

    @Override
    public List<EntityMap> selectHySaleManList(String dbName) {
        return hyRemoteOrderMapper.selectHySaleManList(dbName);
    }

    @Override
    public List<EntityMap> selectHasUpdateHyCustomList(String dbName, EntityMap params) {
        return hyRemoteOrderMapper.selectHasUpdateHyCustomList(dbName, params);
    }

    @Override
    public EntityMap selectHyCustom(String dbName, String customCode) {
        return hyRemoteOrderMapper.selectHyCustom(dbName, customCode);
    }

    @Override
    @DS("hy")
    public List<EntityMap> selectHyCustomAddress(String dbName, String customCode) {
        return hyRemoteOrderMapper.selectHyCustomAddress(dbName, customCode);
    }

    @Override
    public List<EntityMap> selectLotNoByDrugNo(@Param("dbName") String dbName, @Param("drugNo") String drugNo, @Param("codes") List<String> codes) {
        return hyRemoteOrderMapper.selectLotNoByDrugNo(dbName, drugNo, codes);
    }

    @Override
    public EntityMap selectHyDrugSurplusNum(String dbName, String drugNo) {
        return hyRemoteOrderMapper.selectHyDrugSurplusNum(dbName, drugNo);
    }

    @Override
    public EntityMap selectByDrugLotId(String dbName, Integer drugLotId) {
        return hyRemoteOrderMapper.selectByDrugLotId(dbName, drugLotId);
    }

    @Override
    public Integer selectLockCount(String dbName, String drugNo, String drugLotNo, String storeCode) {
        Integer lockCount = hyRemoteOrderMapper.selectLockCount(dbName, drugNo, drugLotNo, storeCode);
        if (FlymeUtils.isEmpty(lockCount)) {
            lockCount = 0;
        }
        return lockCount;
    }

    @Override
    public Integer countCdCount(String dbName, String drugNo, String drugLotNo, String storeCode) {
        return hyRemoteOrderMapper.countCdCount(dbName, drugNo, drugLotNo, storeCode);
    }

    @Override
    public BigDecimal countCddx(String dbName, String drugNo, String drugLotNo, String storeCode, BigDecimal costPrice) {
        BigDecimal num = hyRemoteOrderMapper.countCddx(dbName, drugNo, drugLotNo, storeCode, costPrice);
        if (FlymeUtils.isEmpty(num)) {
            num = new BigDecimal("0");
        }
        return num;
    }

    @Override
    public BigDecimal countOtherCount(String dbName, String drugNo, String drugLotNo, String storeCode, BigDecimal costPrice) {
        return hyRemoteOrderMapper.countOtherCount(dbName, drugNo, drugLotNo, storeCode, costPrice);
    }


    @Override
    public Integer countCkCount(String dbName, String drugNo, String drugLotNo, String storeCode) {
        return hyRemoteOrderMapper.countCkCount(dbName, drugNo, drugLotNo, storeCode);
    }

    @Override
    public BigDecimal countThCount(String dbName, String orderNo, String drugNo, String drugLotNo, String storeCode) {
        return hyRemoteOrderMapper.countThCount(dbName, orderNo, drugNo, drugLotNo, storeCode);
    }

    @Override
    public BigDecimal countThCount1(String dbName, String orderNo, String drugNo, String drugLotNo, String storeCode) {
        return hyRemoteOrderMapper.countThCount1(dbName, orderNo, drugNo, drugLotNo, storeCode);
    }


    @Override
    public List<EntityMap> selectDrugLotList(String dbName, String drugNo, String drugLotNo, String storeCode) {
        return hyRemoteOrderMapper.selectDrugLotList(dbName, drugNo, drugLotNo, storeCode);
    }


    @Override
    public ResultBody stockReport(Map params) {
        int limit = TypeUtils.castToInt(params.get("limit"));
        int page = TypeUtils.castToInt(params.get("page"));
        int endLimit = limit * (page - 1);
        params.put("endLimit", endLimit);
        List<EntityMap> list = hyRemoteOrderMapper.stockReport(params);
        int total = hyRemoteOrderMapper.countStockReport(params);
        Integer pages = Math.toIntExact(total / limit);
        EntityMap pageModel = new EntityMap();
        pageModel.put("size", list.size());
        pageModel.put("total", total);
        pageModel.put("current", page);
        pageModel.put("pages", pages);
        pageModel.put("records", list);
        return ResultBody.ok(pageModel);
    }

    @Override
    public void insertHyOrder(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.insertHyOrder(dbName, entity);
    }

    @Override
    public BigDecimal selectCostPrice(String dbName, String orderDetailsId) {
        return hyRemoteOrderMapper.selectCostPrice(dbName, orderDetailsId);
    }

    @Override
    public void insertHyBackOrder(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.insertHyBackOrder(dbName, entity);
    }

    @Override
    public void insertHyOrderDetails(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.insertHyOrderDetails(dbName, entity);
    }

    @Override
    public void updateByDrugLotId(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateByDrugLotId(dbName, entity);
    }

    @Override
    public void updateOnUserCount(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateOnUserCount(dbName, entity);
    }

    @Override
    public void updateDrugOnUserCount(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateDrugOnUserCount(dbName, entity);
    }

    @Override
    public void updateLastSaleDate(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateLastSaleDate(dbName, entity);
    }

    @Override
    public void updateDrugId(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateDrugId(dbName, entity);
    }


    @Override
    public void deleteOrderByOrderId(String dbName, String orderId) {
        hyRemoteOrderMapper.deleteOrderByOrderId(dbName, orderId);
    }

    @Override
    public void deleteOrderDetailsByOrderId(String dbName, String orderId) {
        hyRemoteOrderMapper.deleteOrderDetailsByOrderId(dbName, orderId);
    }

    @Override
    public void updateXsComments(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateXsComments(dbName, entity);
    }

    @Override
    public void updateZlComments(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateZlComments(dbName, entity);
    }

    @Override
    public void updateCwComments(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateCwComments(dbName, entity);
    }

    @Override
    public void updateJsComments(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateJsComments(dbName, entity);
    }

    @Override
    public void updateCostPrice(String dbName) {
        hyRemoteOrderMapper.updateCostPrice(dbName);
    }


    @Override
    public void updateOrderDetailsSysProperties(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateOrderDetailsSysProperties(dbName, entity);
    }

    @Override
    public void updateSyncState(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateSyncState(dbName, entity);
    }

    @Override
    public EntityMap selectSysProperties(String dbName, String hostName) {
        return hyRemoteOrderMapper.selectSysProperties(dbName, hostName);
    }

    @Override
    public void updateOrderDetailsId(String dbName, String hostName, String detailsId, String orderId) {
        EntityMap sysProperties = selectSysProperties(dbName, hostName);
        String lastOrderDetailsId = sysProperties.get("orderDetailsId");
        //当前订单明细尾号
        Integer detailsNum = Integer.parseInt(detailsId.substring(4));
        //最新的订单明细尾号
        Integer lastOrderDetailsCount = 0;
        if (FlymeUtils.isNotEmpty(lastOrderDetailsId)) {
            lastOrderDetailsCount = Integer.parseInt(lastOrderDetailsId.substring(4));
        }
        if (detailsNum > lastOrderDetailsCount) {
            EntityMap params = new EntityMap();
            params.put("hostName", hostName);
            params.put("orderDetailsId", detailsId);
            params.put("orderNo", orderId);
            updateOrderDetailsSysProperties(dbName, params);
        }
    }

    @Override
    public EntityMap selectHyDeptByDeptNo(String dbName, String deptNo) {
        return hyRemoteOrderMapper.selectHyDeptByDeptNo(dbName, deptNo);
    }

    @Override
    public void insertHySaleMan(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.insertHySaleMan(dbName, entity);
    }

    @Override
    public void updateHySaleMan(String dbName, EntityMap entity) {
        hyRemoteOrderMapper.updateHySaleMan(dbName, entity);
    }

    @Override
    public Integer countByOrderId(String dbName, String orderId) {
        return hyRemoteOrderMapper.countByOrderId(dbName, orderId);
    }

    @Override
    public Integer countBySaleManNo(String dbName, String saleManNo) {
        return hyRemoteOrderMapper.countBySaleManNo(dbName, saleManNo);
    }

    @Override
    public Integer deleteSaleManByNo(String dbName, String saleManNo) {
        return hyRemoteOrderMapper.deleteSaleManByNo(dbName, saleManNo);
    }

    @Override
    public Map getOutOrderNo(String dbName, String orderId) {
        return hyRemoteOrderMapper.getOutOrderNo(dbName, orderId);
    }

    @Override
    public Map getCkjOutOrderNo(String dbName, String orderId) {
        String orderNo = hyRemoteOrderMapper.getChhOutOrderNo(dbName, orderId);
        if (FlymeUtils.isNotEmpty(orderNo)) {
            return getOutOrderNo(dbName, orderNo);
        }
        return null;
    }


    @Override
    public ResultBody hyOrderVoucherList(Map params) {
        int limit = TypeUtils.castToInt(params.get("limit"));
        int page = TypeUtils.castToInt(params.get("page"));
        int endLimit = limit * (page - 1);
        params.put("endLimit", endLimit);
        //params.put("dbName", "DBR_YP2018.dbo.");
        Integer orderType = MapUtil.getInt(params, "orderType");
        List<EntityMap> list = new ArrayList<>();
        int total = 0;
        if (VoucherTypeEnum.Cg.getCode().equals(orderType)) {
            list = hyRemoteOrderMapper.cgList(params);
            total = hyRemoteOrderMapper.cgCount(params);
        }
        if (VoucherTypeEnum.Xs.getCode().equals(orderType)) {
            list = hyRemoteOrderMapper.xsList(params);
            total = hyRemoteOrderMapper.xsCount(params);
        }
        if (VoucherTypeEnum.Cb.getCode().equals(orderType)) {
            list = hyRemoteOrderMapper.cbList(params);
            total = hyRemoteOrderMapper.cbCount(params);
        }
        if (VoucherTypeEnum.Yp.getCode().equals(orderType)) {
            list = hyRemoteOrderMapper.ypList(params);
            total = hyRemoteOrderMapper.ypCount(params);
        }
        if (VoucherTypeEnum.Fk.getCode().equals(orderType)) {

        }
        if (VoucherTypeEnum.Sk.getCode().equals(orderType)) {

        }


        Integer pages = Math.toIntExact(total / limit);
        EntityMap pageModel = new EntityMap();
        pageModel.put("size", list.size());
        pageModel.put("total", total);
        pageModel.put("current", page);
        pageModel.put("pages", pages);
        pageModel.put("records", list);
        return ResultBody.ok(pageModel);
    }

    @Override
    public EntityMap getHyOrderVoucherByOrderNo(Map params) {
        String orderNo = MapUtil.getStr(params, "orderNo");
        String dbName = MapUtil.getStr(params, "dbName", "DBR_YP2018.dbo.");
        Integer orderType = MapUtil.getInt(params, "orderType", 2);
        List<EntityMap> list = new ArrayList<>();

        if (VoucherTypeEnum.Cg.getCode().equals(orderType)) {
            list = hyRemoteOrderMapper.buyOrderDetailList(dbName, null, Arrays.asList(), orderNo);
        }
        if (VoucherTypeEnum.Xs.getCode().equals(orderType) || VoucherTypeEnum.Cb.getCode().equals(orderType)) {
            list = hyRemoteOrderMapper.saleOrderDetailList(dbName, null, Arrays.asList(), orderNo);
        }
        if (VoucherTypeEnum.Yp.getCode().equals(orderType)) {
            list = busErpOrderDetailsService.selectListByOrderNo(dbName,orderNo);
        }
        EntityMap pageModel = new EntityMap();
        pageModel.put("size", list.size());
        pageModel.put("total", list.size());
        pageModel.put("current", 1);
        pageModel.put("pages", list.size());
        pageModel.put("records", list);
        return pageModel;
    }

    @Override
    public ResultBody createVoucher(Map params) {
        String dbName = MapUtil.getStr(params, "dbName", "");
        String code = MapUtil.getStr(params, "companyCode", "");
        Long companyId = MapUtil.getLong(params, "companyId", 0L);
        List<String> orderNos = StrUtil.split(params.get("orderNo").toString(), ',');
        syncService.createVoucher(companyId, dbName, code, orderNos, MapUtil.getStr(params, "orderType"), OpenHelper.getUser(), MapUtil.getStr(params, "day", ""));
        return ResultBody.ok();
    }

    @Override
    public ResultBody mergeVoucher(Map params) {
        String dbName = MapUtil.getStr(params, "dbName", "");
        String code = MapUtil.getStr(params, "companyCode", "");
        Long companyId = MapUtil.getLong(params, "companyId", 0L);
        List<String> orderNos = StrUtil.split(params.get("orderNo").toString(), ',');
        syncService.mergeVoucher(companyId, dbName, code, orderNos, MapUtil.getStr(params, "orderType"), OpenHelper.getUser(), MapUtil.getStr(params, "day", ""));
        return ResultBody.ok();
    }

    @Override
    public ResultBody monthVoucher(Map params) {
        String dbName = MapUtil.getStr(params, "dbName", "");
        String code = MapUtil.getStr(params, "companyCode", "");
        Long companyId = MapUtil.getLong(params, "companyId", 0L);
        syncService.monthVoucher(companyId, dbName, code, null, MapUtil.getStr(params, "orderType"), OpenHelper.getUser(), MapUtil.getStr(params, "day", ""));
        return ResultBody.ok();
    }

    @Override
    public void updateXsVocher(String dbName, int state, String voucher, List<String> orderNos) {
        hyRemoteOrderMapper.updateXsVocher(dbName, state, voucher, orderNos);
    }

    @Override
    public void updateCbVocher(String dbName, int state, String voucher, List<String> orderNos) {
        hyRemoteOrderMapper.updateCbVocher(dbName, state, voucher, orderNos);
    }

    @Override
    public void updateCgVocher(String dbName, int state, String voucher, List<String> orderNos) {
        hyRemoteOrderMapper.updateCgVocher(dbName, state, voucher, orderNos);
    }

    @Override
    public void updateYpVocher(String dbName, int state, String voucher, List<String> orderNos) {
        hyRemoteOrderMapper.updateYpVocher(dbName, state, voucher, orderNos);

    }

    @Override
    public List<EntityMap> monthOrderList(Map params) {

        if (VoucherTypeEnum.Yp.getCode().equals(MapUtil.getInt(params, "orderType"))) {
            params.put("yp", "yp");

        }
        if (VoucherTypeEnum.Cb.getCode().equals(MapUtil.getInt(params, "orderType"))) {
        }

        return hyRemoteOrderMapper.xsMonthList(params);
    }
}
